
# This file estimates if twinning after 2009 predicts voting in 2009 --------


# install.packages("dplyr")
# install.packages("date")
# install.packages("AER")

library(dplyr)
library(date)
library(AER)

# Load data
load("mothers.rdata")
load("fathers.rdata")

# Filter of if age of oldest is unkwown (select parents )
mothers <-
  mothers %>%
  filter(!is.na(age_oldest)) 

# select 2009 election
# Subset and create vote variable
# filter to parents with children born after the election
# After around 10 weeks of pregnancy the mother has her first scan
# where she learns if she is expecting twins.
# Because parents might be affected by knowing they will twin, 
# I subset to parents who had a child 9 months minus 10 weeks later than the election

mothers09 <- 
  mothers %>%
  filter(age_oldest > (14565 + 273 - 70)) %>%
  filter(! is.na(stemte_2009) & first09 > 0) %>%
  mutate(vote = stemte_2009 == "Stemte")

#create age dummies for mothers 
#dichotomize twin first
mothers09 <-
  within(mothers09, {
    age        <- ntile(date, 20)
    twin_first <- twin_first > 0
  })

# create dummies for age of children grouped by age of mother dummies
mothers09 <-
  mothers09 %>%
  group_by(age) %>%
  mutate(child_age = ntile(age_oldest, 2))

mod09_r_mom <- lm(vote ~ twin_first, data = mothers09)
mod09_mom   <- lm(vote ~ twin_first + as.factor(age) * as.factor(child_age), data = mothers09)
mod09_iv_mom  <- ivreg(vote ~ no_children_09 + as.factor(age) * as.factor(child_age) | 
                     twin_first + as.factor(age) * as.factor(child_age), data = mothers09)

# Repeat for fathers

fathers09 <- 
  fathers %>%
  filter(age_oldest > (14565 + 273 - 70)) %>%
  filter(! is.na(stemte_2009) & first09 > 0) %>%
  mutate(vote = stemte_2009 == "Stemte")

#create age dummies for fathers 
#dichotomize twin first
fathers09 <-
  within(fathers09, {
    age        <- ntile(date, 20)
    twin_first <- twin_first > 0
  })

# create dummies for age of children grouped by age of father dummies
fathers09 <-
  fathers09 %>%
  group_by(age) %>%
  mutate(child_age = ntile(age_oldest, 2))

mod09_r_dad <- lm(vote ~ twin_first, data = fathers09)
mod09_dad   <- lm(vote ~ twin_first + as.factor(age) * as.factor(child_age), data = fathers09)
mod09_iv_dad  <- ivreg(vote ~ no_children_09 + as.factor(age) * as.factor(child_age) | 
                     twin_first + as.factor(age) * as.factor(child_age), data = fathers09)

# compile results

table_mom_09 <-
  rbind(summary(mod09_r_mom)$coefficients,
        summary(mod09_mom)$coefficients[2,],
        summary(mod09_iv_mom)$coefficients[2,])

table_dad_09 <-
  rbind(summary(mod09_r_dad)$coefficients,
        summary(mod09_dad)$coefficients[2,],
        summary(mod09_iv_dad)$coefficients[2,])

ns <- rbind(
  with(mothers09, table(twin_first)),
  with(fathers09, table(twin_first))
)

outtab_placebo <- 
  list(table_mom_09,
       table_dad_09,
       ns)

save(outtab_placebo, file = "twin_placebo.rdata")

